package edu.hubu.helloworld;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import edu.hubu.helloworld.fragemnt.DailyFragment;
import edu.hubu.helloworld.fragemnt.FriendFragment;
import edu.hubu.helloworld.fragemnt.MessageFragment;
import edu.hubu.helloworld.fragemnt.MyFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    TextView textView;

    LinearLayout message, friend, daily, my;
    FrameLayout content;
    // define and init, don't use getFragmentManager() method.
    FragmentManager fragmentManager = getSupportFragmentManager();
    // define fragment
    Fragment messageFragment = new MessageFragment();
    Fragment friendFragment = new FriendFragment();
    Fragment dailyFragment = new DailyFragment();
    Fragment myFragment = new MyFragment();
    // define image
    ImageView messageImage;
    ImageView friendImage;
    ImageView dailyImage;
    ImageView myImage;


    private void init() {
        initFragment();
        initImage();
    }

    private void initFragment() {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.content, messageFragment);
        fragmentTransaction.add(R.id.content, friendFragment);
        fragmentTransaction.add(R.id.content, dailyFragment);
        fragmentTransaction.add(R.id.content, myFragment);
        this.hideAllFragment(fragmentTransaction);
        fragmentTransaction.show(messageFragment);
        fragmentTransaction.commit();
    }

    private void initImage() {
        messageImage = findViewById(R.id.msg_imageView);
        friendImage = findViewById(R.id.friend_imageView);
        dailyImage = findViewById(R.id.daily_imageView);
        myImage = findViewById(R.id.my_imageView);
        resetImage();
        messageImage.setImageResource(R.drawable.chat_active);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout_main);
        // init
        this.init();
        // bind view
        message = findViewById(R.id.message);
        friend = findViewById(R.id.friend);
        daily = findViewById(R.id.daily);
        my = findViewById(R.id.my);
        content = findViewById(R.id.content);
        // set listener
        message.setOnClickListener(this);
        friend.setOnClickListener(this);
        daily.setOnClickListener(this);
        my.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Log.v("fw", "爬");
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        hideAllFragment(fragmentTransaction);
        resetImage();
        switch (v.getId()) {
            case R.id.message:
                Log.v("fw", "拉了");
                fragmentTransaction.show(this.messageFragment);
                messageImage.setImageResource(R.drawable.chat_active);
                break;
            case R.id.friend:
                Log.v("fw", "天街巡游!斩尽牛杂!");
                fragmentTransaction.show(this.friendFragment);
                friendImage.setImageResource(R.drawable.friend_active);
                break;
            case R.id.daily:
                Log.v("fw", "你在忙吗?还是在摸鱼");
                fragmentTransaction.show(this.dailyFragment);
                dailyImage.setImageResource(R.drawable.daily_active);
                break;
            case R.id.my:
                Log.v("fw", "好耶!");
                fragmentTransaction.show(this.myFragment);
                myImage.setImageResource(R.drawable.my_active);
                break;
        }
        fragmentTransaction.commit();
    }

    private void hideAllFragment(FragmentTransaction fragmentTransaction) {
        fragmentTransaction.hide(messageFragment);
        fragmentTransaction.hide(friendFragment);
        fragmentTransaction.hide(dailyFragment);
        fragmentTransaction.hide(myFragment);
    }

    private void resetImage() {
        messageImage.setImageResource(R.drawable.chat);
        friendImage.setImageResource(R.drawable.friend);
        dailyImage.setImageResource(R.drawable.daily);
        myImage.setImageResource(R.drawable.my);
    }


}